package com.czk.model.proxy.dynamincproxy.practice2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/10/2 19:46
 */
public class ProxyFactory {
    private Object targetClass;

    public ProxyFactory(Object targetClass) {
        this.targetClass = targetClass;
    }

    public Object getProxyObject() {
        return Proxy.newProxyInstance(targetClass.getClass().getClassLoader(), targetClass.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("开始代理了");
                method.invoke(targetClass, args);
                System.out.println("代理结束了");
                return proxy;
            }
        });
    }
    private ProxyFactory(){}
}
